import requests
import os

HEADERS = {
    "method": "POST",
    "version": "HTTP/1.1",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate",
    "accept-language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) "
}

formdata = {
    "name": "xiaoming",
    "age": 12
}

files = [
    # "resume"为params的key, params的value为tuple, tuple[0]为文件别名,  tuple[1]为文件对象
    ("resume", ('haha_doc', open("F:/doc/haha.doc", "rb"))),
    ("photo", ('123_jpg', open("F:/123.jpg", "rb"), "image/jpeg"))
]

'''
# 也可以直接用简洁形式, 文件名此时使用默认文件名(文件的全路径) 不推荐
files = [
    ("resume", open("F:/doc/haha.doc", "rb")),
    ("photo",  open("F:/123.jpg", "rb"))
]
'''

# 1. 多对多多文件post方法的files参数接收2种数据格式: dict与tuple
# tuple (统一用tuple,无视dict)
response1 = requests.post(url, data=formdata, headers=HEADERS, files=files)


# 2. 一对多多文件post (multifiles)
multi_files = [
    ("photos", ('1_jpg', open('F:/1.jpg','rb'))),
    ("photos", ('2_jpg', open('F:/2.jpg','rb'))),
    ("photos", ('3_jpg', open('F:/3.jpg','rb')))
]
response2 = requests.post(url, data=formdata, headers=HEADERS, files=multi_files)
